#region license
/*
 * Copyright (C) 2007 CchenSoft.com
 * 
 * See http://www.cchensoft.com/opensource/cc-portal/license.txt
 * 
 * Author: Billy Zhang
 * Email: billy_zh@126.com
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Net;
using System.IO;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI.HtmlControls;
using CchenSoft.Portal.Model;

namespace CchenSoft.Portal.Web.Controls
{
    [ParseChildren(true)]
    public class PortletView : ThemeControl
    {
        private PortletInstance portlet;

        public PortletView(PortletInstance portlet)
        {
            this.portlet = portlet;
            SkinFileName = "portletview.ascx";
        }

        //protected override 
        protected override void InitializeSkin(Control control)
        {
            Literal title = (Literal)control.FindControl("title");
            PlaceHolder ph = (PlaceHolder)control.FindControl("content");

            title.Text = portlet.Title;

            try
            {
                HtmlContainerControl box = (HtmlContainerControl)control.FindControl("portlet_box");
                if (box != null)
                    box.ID = "_" + portlet.Id;

                HtmlContainerControl box_h = (HtmlContainerControl)control.FindControl("portlet_box_h");
                if (box_h != null)
                    box_h.ID = "_" + portlet.Id + "_h";

                HtmlContainerControl box_c = (HtmlContainerControl)control.FindControl("portlet_box_c");
                if (box_c != null)
                    box_c.ID = "_" + portlet.Id + "_c";

                HyperLink hlnkCfg = (HyperLink)control.FindControl("hlnkCfg");
                HyperLink hlnkClose = (HyperLink)control.FindControl("hlnkClose");
                if (PortalContext.Current.IsAdmin)
                {
                    hlnkCfg.NavigateUrl = string.Format(hlnkCfg.NavigateUrl, portlet.Id);
                    hlnkClose.NavigateUrl = string.Format(hlnkClose.NavigateUrl, box.ClientID);
                }
                else
                {
                    hlnkCfg.Visible = false;
                    hlnkClose.Visible = false;
                    box_h.Visible = portlet.ShowTitle;
                }

                PortletControl pc = PortletFactory.Create(portlet);

                if (pc != null)
                {
                    ph.Controls.Add(pc);
                }
            }
            catch (Exception ex)
            {
                Literal lt = new Literal();
                lt.Text = ex.ToString().Replace(Environment.NewLine, "<br/>");
                ph.Controls.Add(lt);
            }
        }
    }
}
